package cn.wolfcode.controller;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Destination;
import cn.wolfcode.query.DestinationQuery;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")DestinationQuery qo){
        Page<Destination> page = destinationService.query(qo);
        model.addAttribute("page", page);

        List<Destination> list = destinationService.queryToasts(qo.getParentId());
        model.addAttribute("toasts", list);

        return "/destination/list";
    }

    @RequestMapping("/listByAll")
    @ResponseBody
    public Object listByAll(){
        return destinationService.findAll();
    }



//    @RequestMapping("/saveOrUpdate")
//    @ResponseBody
//    public Object saveOrUpdate(Destination destination){
//
//        destinationService.saveOrUpdate(destination);
//        return JsonResult.success();
//    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        destinationService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id, String info){
        Destination destination = destinationService.get(id);
        destination.setInfo(info);
        destinationService.update(destination);
        return JsonResult.success();
    }



}
